﻿using HYBY.Task.DataEntity;
using HYBY.Task.Service.IService;
using HYBY.WebFrameWork.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Models;

namespace Web.Controllers
{
    public class TAModularController : BaseController
    {


        private readonly ITAModularService _service;

        private readonly ITAStructureService _services;


        public TAModularController(ITAModularService service, ITAStructureService services)
        {
            _service = service;
            _services = services;
        }

        //
        // GET: /TAModular/
        public ActionResult Index()
        {
            var list = _service.GetTAModulars();

            //模块类型
            ViewBag.ModularTypeId = ModularTypeId();

            //产品
            ViewBag.ProductId = ProductId();

            //模块
            ViewBag.ProductModelId = ProductModelId();

            return View(list);
        }


        /// <summary>
        /// 修改或保存
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type">添加或修改 1 添加 2 修改</param>
        /// <param name="section">环节</param>
        /// <returns></returns>
        public ActionResult AddOrUpdate(int dataid, int type)
        {
            var model = new TAModularModel();
            

            //修改
            if (dataid > 0)
            {


                if (type == 1)
                {
                    var query = _service.Get(dataid);
                    model.ModularTypeId = query.ModularTypeId + 1;
                    model.ProductId = query.ProductId;
                    model.FatherId = Convert.ToInt32(query.ID);
                    model.Id = 0;
                }
                else
                {
                    var query = _service.Get(dataid);
                    model = new TAModularModel(query);
                }

            }

            ViewBag.UserInfo = GerUserList();

            return View(model);
        }

        public ActionResult Save(TAModularModel model)
        {

            string ModularName = model.ModularTypeId == 1 ? ProductId()[model.ProductId] : model.ModularName;

            var res = _service.Save(new TAModularEntity()
            {
                ID = model.Id,
                ModularTypeId = model.ModularTypeId,
                FatherId = model.FatherId,
                ProductId = model.ProductId,
                ModularName = ModularName,
                ResearchHeadCard = model.ResearchHeadCard,
                ResearchHead = model.ResearchHead,
                UEDHeadCard = model.UEDHeadCard,
                UEDHead = model.UEDHead,
                DevelopmentHeadCard = model.DevelopmentHeadCard,
                DevelopmentHead = model.DevelopmentHead,
                TestHeadCard = model.TestHeadCard,
                TestHead = model.TestHead,
                ApplicationHeadCard = model.ApplicationHeadCard,
                ApplicationHead = model.ApplicationHead,
                AddTime = DateTime.Now
            });



            return Json(new { isSuccess= res });
        }

        /// <summary>
        /// 模块类型
        /// </summary>
        /// <returns></returns>
        public Dictionary<int, string> ModularTypeId()
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();

            dic.Add(1, "产品线");
            dic.Add(2, "系统");
            dic.Add(3, "模块");
            dic.Add(4, "科目");

            return dic;
        }

        /// <summary>
        /// 产品
        /// </summary>
        /// <returns></returns>
        public Dictionary<int, string> ProductId()
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();

            dic.Add(1, "商务");
            dic.Add(2, "主案");
            dic.Add(3, "工程");
            dic.Add(4, "材料");
            dic.Add(5, "造价");
            dic.Add(6, "财务");
            dic.Add(7, "人事");
            dic.Add(8, "家具弱电智能硬件");

            return dic;
        }

        /// <summary>
        /// 模块
        /// </summary>
        /// <returns></returns>
        public Dictionary<int, string> ProductModelId()
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();

            dic.Add(1, "销售");
            dic.Add(2, "生产");
            dic.Add(3, "产品");
            dic.Add(4, "地区系统");

            return dic;

        }

        public ActionResult GetUserInfo(int id)
        {
            return View();
        }

        //查询
        public ActionResult GetChild(int fatherId, int productId, int modularTypeId)
        {

            if (productId == 0 || modularTypeId == 0)
            {
                return View();
            }

            var childList = _service.GetTAModularByWhere(new TAModularEntity() { FatherId = fatherId, ProductId = fatherId, ModularTypeId = modularTypeId });

            return Json(childList);
        }

        public SelectList GerUserList()
        {
            var list = _services.GetTAStructures();

            SelectList sList = new SelectList(list, "Card", "Name");

            return sList;
        }

    }
}